package com.stlm2.controller;

import com.stlm2.dems.service.DdmGoodsService;
import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by zlq on 2017/10/18.
 */
@Controller
@RequestMapping("/ddmGoodsC")
public class DdmGoodsController {
    @Resource
    private DdmGoodsService ddmGoodsService;

    /*sfy
* crm添加商品到某个店铺
* */
    @RequestMapping(value="/addInventoryToStore",produces="application/json;charset=UTF-8")
    public ModelAndView addInventoryToStore(ModelAndView modelAndView){
        modelAndView.setViewName("/inventory/addInventoryToStore");
        return modelAndView;
    }
    /*zlq
* crm查询商品列表页面
* */
    @RequestMapping(value="/listView",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/goods/list");
        return modelAndView;
    }

    /*zlq
* crm添加商品页面
* */
    @RequestMapping(value="/addView",produces="application/json;charset=UTF-8")
    public ModelAndView addView(ModelAndView modelAndView){
        modelAndView.setViewName("/goods/add");
        return modelAndView;
    }
    /*zlq
* crm修改商品页面
* */
    @RequestMapping(value="/modificationView",produces="application/json;charset=UTF-8")
    public ModelAndView modificationView(ModelAndView modelAndView,String id){
        modelAndView.setViewName("/goods/modification");
        modelAndView.addObject("id",id);
        return modelAndView;
    }

    /*zlq
* crm商品详情页面
* */
    @RequestMapping(value="/detailsView",produces="application/json;charset=UTF-8")
    public ModelAndView detailsView(ModelAndView modelAndView,String id){
        modelAndView.setViewName("/goods/details");
        modelAndView.addObject("id",id);
        return modelAndView;
    }
    /*
    *zlq
    * 添加商品
    * token:
    * abbreviation:  商品简称
    * name: 商品名称
    * mainImg: 商品主图
    * detailsImg: 商品详情图
    * goodsExplain: 商品说明
    * purchasePrice: 进货价
    * costPrice: 成本价
    * ddPrice:  叮当价
    * memberPrice：会员价
    * isWarranty：是否保修
    * warrantyPeriod： 保修期(单位：月)
    * isImie：是否有唯一码
    * parameter:详细参数
    * safeguard:售后保障
     */
    @ResponseBody
    @RequestMapping(value="/add.json",produces="application/json;charset=UTF-8")
    public  String add(ModelMap model, String token,String abbreviation, String name ,String mainImg,
                       String detailsImg,String goodsExplain,String purchasePrice,String costPrice,
                       String ddPrice,String memberPrice,boolean isWarranty ,String warrantyPeriod,
                       boolean isImie,String parameter,String safeguard,String keyWord){
        String data=null;
        try {
            data=ddmGoodsService.add(model,token,abbreviation,name,mainImg,detailsImg,goodsExplain,purchasePrice,
                    costPrice,ddPrice,memberPrice, isWarranty,warrantyPeriod,isImie,parameter,safeguard,keyWord);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"添加失败",data);
        }
        return  data;
    }
    /*
    *修改商品
    * zlq
    * token:
    * abbreviation: 商品简称
    * name: 商品名称
    * mainImg: 商品主图
    * detailsImg: 商品详情图
    * goodsExplain: 商品说明
    * purchasePrice: 进货价
    * costPrice: 成本价
    * ddPrice: 叮当价
    * memberPrice: 会员价
    * id: 商品ID
    * isWarranty：是否保修
    * warrantyPeriod： 保修期(单位：月)
    * isImie：是否有唯一码
    * parameter:详细参数
    * safeguard:售后保障
     */
    @ResponseBody
    @RequestMapping(value="/goodsModification.json",produces="application/json;charset=UTF-8")
    public  String goodsModification(ModelMap model, String token,String abbreviation, String name ,String mainImg,
                                     String detailsImg,String goodsExplain,String purchasePrice,String costPrice,
                                     String ddPrice,String memberPrice,String id,boolean isWarranty,String warrantyPeriod,
                                     boolean isImie,String parameter,String safeguard,String keyWord){
        String data=null;
        try {
            data=ddmGoodsService.goodsModification(model,token,abbreviation,name,mainImg,detailsImg,
                    goodsExplain,purchasePrice,costPrice,ddPrice,memberPrice,id,isWarranty,warrantyPeriod,isImie,parameter,safeguard,keyWord);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"修改失败",data);
        }
        return  data;
    }
    /*
*zlq
* crm 查询商品列表
* page:页码
* token：
* code：商品编码
* name：商品名称
* abbreviation：商品简称
*state:商品状态
 */
    @ResponseBody
    @RequestMapping(value="/getCrmGoodsList.json",produces="application/json;charset=UTF-8")
    public  String getCrmGoodsList(ModelMap model, String page,String token,String code ,String name,String abbreviation,Boolean state){
        String data=null;
        try {
            data=ddmGoodsService.getCrmGoodsList( model,page,token,code ,name, abbreviation,state);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }


    	/*
*zlq
* crm 查询所有的商品列表的下拉框
* code：商品编码
* name：商品名称
* abbreviation：商品简称
 */

    @ResponseBody
    @RequestMapping(value="/getCrmAllGoodsListSelect.json",produces="application/json;charset=UTF-8")
    public  String getCrmAllGoodsListSelect(ModelMap model,String code ,String name,String abbreviation){
        String data=null;
        try {
            data=ddmGoodsService.getCrmAllGoodsListSelect(model,code,name,abbreviation);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }

    /*
    *crm获取商品详情
    * zlq
    * token:
    * id:
     */
    @ResponseBody
    @RequestMapping(value="/getCrmDetails.json",produces="application/json;charset=UTF-8")
    public  String getCrmDetails(ModelMap model,String id,String token){
        String data=null;
        try {
            data=ddmGoodsService.getCrmDetails(model,id,token);
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }

    /*
    *商品上下架
    * zlq
    * token:
    * id: 商品ID
    * state : true:上架 false：下架
     */
    @ResponseBody
    @RequestMapping(value="/updateState.json",produces="application/json;charset=UTF-8")
    public  String updateState(ModelMap model,String token, String id,boolean state ){
        String data=null;
        try {
            data=ddmGoodsService.updateState(model,token,id,state );
        }catch (Exception e){
            e.printStackTrace();
            return  CodeContent.getModelByCode(0,"查询失败",data);
        }
        return  data;
    }
}
